package com.jxstyle.config;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.beetl.core.GroupTemplate;
import org.beetl.core.ResourceLoader;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.jxstyle.core.properties.JxsConfigProperties;

@Configuration
public class BeetlConf {

	@Autowired
	private JxsConfigProperties jxsConfigProperties;

	/**
	 * <dt><span class="strong">方法描述:</span></dt>
	 * <dd>声明需要暴露给页面的变量</dd>
	 * <dt><span class="strong">作者:</span></dt>
	 * <dd>weijx</dd>
	 * <dt><span class="strong">时间:</span></dt>
	 * <dd>2018年12月28日 下午5:09:07</dd>
	 * 
	 * @return
	 * @since v1.0
	 */
	public Map<String, Object> getSharedVars() {
		Map<String, Object> vars = new HashMap<>();
		vars.put("cdn_url", jxsConfigProperties.getCdn_url());
		return vars;
	}

	@Bean(name = "resourceLoader")
	public ResourceLoader getResourceLoader() throws IOException {
		return new ClasspathResourceLoader("templates/");
	}

	@Bean(name = "classLoader")
	public ClassLoader getClassLoader() throws IOException {
		return BeetlConf.class.getClassLoader();
	}

	@Bean(name = "groupTemplate")
	public GroupTemplate getGroupTemplate(@Qualifier("classLoader") ClassLoader classLoader) throws IOException {
		GroupTemplate groupTemplate = new GroupTemplate();
		groupTemplate.setClassLoader(classLoader);
		groupTemplate.setSharedVars(this.getSharedVars());
		return groupTemplate;
	}

	@Bean(name = "beetlConfig")
	public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration(
			@Qualifier("groupTemplate") GroupTemplate groupTemplate,
			@Qualifier("resourceLoader") ResourceLoader resourceLoader) throws IOException {
		BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
		beetlGroupUtilConfiguration.setResourceLoader(resourceLoader);
		beetlGroupUtilConfiguration.setSharedVars(groupTemplate.getSharedVars());
		Properties configProperties = new Properties();
//		configProperties.put("DELIMITER_STATEMENT_START", "@");
//		configProperties.put("DELIMITER_STATEMENT_END", "");
		beetlGroupUtilConfiguration.setConfigProperties(configProperties);
		beetlGroupUtilConfiguration.config(groupTemplate);
		beetlGroupUtilConfiguration.init();
		return beetlGroupUtilConfiguration;

	}

	@Bean(name = "beetlViewResolver")
	public BeetlSpringViewResolver getBeetlSpringViewResolver(
			@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
		BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
		beetlSpringViewResolver.setSuffix(".html");// 设置beetl的后缀设置为btl
		beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
		beetlSpringViewResolver.setOrder(0);
		beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
		return beetlSpringViewResolver;
	}

}